package com.example.demo.webtest;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @auther hewenhui
 * @see <a href="hetp://www.codingmaster.com> java </a>
 */
public class QQmusicians {
    WebDriver webDriver = null;

    @Before
    public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        webDriver = new ChromeDriver(options);
        webDriver.manage().window().maximize();
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    @After
    public void tearDown() throws Exception {
//        if(webDriver!=null)
//            webDriver.quit();
    }
    @Test
    public void testQQmusicians() throws InterruptedException, AWTException {
        // 1、qq音乐首页（未登录）
        webDriver.get("https://y.qq.com/");
        String firstTabHandle = webDriver.getWindowHandle();
        Thread.sleep(2000);
        // 2、拖动鼠标、悬停、点击上一页、点击下一页
        // 因为[上一页]和[下一页]是隐藏的，所以先把鼠标移动到【为你推荐】将隐藏按钮触发，或者直接将鼠标移动到对应位置悬停触发
        WebElement recommendButton = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[1]/div[2]/a[1]"));
        Actions actions = new Actions(webDriver);
        actions.moveToElement(recommendButton);
        Thread.sleep(2000);
        WebElement previous = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/div[1]/a"));
        actions.moveToElement(previous);
        actions.click().perform();
        Thread.sleep(2000);
        WebElement nextPage = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/div[2]/a"));
        actions.click().perform();
        Thread.sleep(2000);
        // 3、点击歌手
        webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/ul[2]/li[2]/a")).click();
        Thread.sleep(2000);
        // 4、点击登录，弹出登录窗口（需要iframe两次）
        webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/div[2]/span/a")).click();
        Thread.sleep(1000);
        WebElement iframe1 = webDriver.findElement(By.xpath("//*[@id=\"login_frame\"]"));
        webDriver.switchTo().frame(iframe1);
        WebElement iframe2 = webDriver.findElement(By.xpath("//*[@id=\"ptlogin_iframe\"]"));
        webDriver.switchTo().frame(iframe2);
        webDriver.findElement(By.xpath("//*[@id=\"switcher_plogin\"]")).click();
        // 5、以密码的形式登录
        // 5.1、输入账号
        WebElement u = webDriver.findElement(By.id("u"));
        u.clear();
        u.sendKeys("204997867");
        // 5.2、输入密码
        WebElement p = webDriver.findElement(By.id("p"));
        p.clear();
        p.sendKeys("hewenhui20021221");
        // 5.3、点击登录
        webDriver.findElement(By.id("login_button")).click();
        // 6、点击第一个歌手
        // 6.1、歌手栏
        Thread.sleep(2000);
        webDriver.findElement(By.xpath("/html/body/div/div/div[1]/div/ul[2]/li[2]/a")).click();
        // 6.2、周杰伦
        Thread.sleep(2000);
        WebElement ZHOU_JIE_LUN = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[3]/div[2]/ul/li[1]/div/a/img"));
        actions.moveToElement(ZHOU_JIE_LUN);
        actions.click().perform();
        // 7、第四首歌曲
        Thread.sleep(2000);
        WebElement song = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[2]/ul[2]/li[4]/div/div[2]/span/a/span"));
        actions.moveToElement(song);
        actions.click().perform();
        // 8、播放
        Thread.sleep(2000);
        webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[1]/div/div[3]/a[1]/span")).click();
        // 9、窗口切换，点击【我知道了】
        Set<String> handles = webDriver.getWindowHandles();
        // 切换到新窗口
        for (String handle : handles) {
            if (!handle.equals(webDriver.getWindowHandle())) {
                webDriver.switchTo().window(handle);
                break;
            }
        }
        webDriver.findElement(By.xpath("/html/body/div[3]/div/div[2]/div/div[2]/div/div/div[3]/button")).click();
        // 10、点击播放键暂停
        Thread.sleep(5000);
        webDriver.findElement(By.xpath("//*[@id=\"app\"]/div[4]/div[3]/a[2]")).click();
        // 11、关闭播放页面、回到第一个页面
        webDriver.close();
        webDriver.switchTo().window(firstTabHandle);
        // 12、点击评论，点击输入框
        webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[1]/div/div[3]/a[4]")).click();


        WebElement divElement  = webDriver.findElement(By.xpath("//*[@id=\"comment_box\"]/div[2]/div[1]/div[1]/div[2]"));
        // 13、输入文字,发送评论
        String contentToInsert = "我爱周董!";
        String script = "var element = arguments[0];" +
                "var content = arguments[1];" +
                "var textNode = document.createTextNode(content);" +
                "element.appendChild(textNode);";
        ((JavascriptExecutor) webDriver).executeScript(script, divElement, contentToInsert);
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"comment_box\"]/div[2]/div[2]/a"));
        actions.moveToElement(element);
        actions.click().perform();
        // 14、点击返回
        webDriver.navigate().back();
        // 15、搜索框架
        WebElement su = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/div[1]/div[1]/input"));
        su.clear();
        su.sendKeys("Don't Be So Serious");
        su.sendKeys(Keys.ENTER);
        // 17、断言
        WebElement name = webDriver.findElement(By.xpath("//*[@id=\"zhida_singer\"]/div/div[1]/h2/a"));
        Assert.assertEquals("Low Roar",name.getText());
    }

}
